<?php


namespace app\api\controller;

use think\facade\Db;

class Role
{
    //获取菜单
    public function getRoutes(){
        //先取出当前管理员的id
        $token = request()->header('token');
        $checkToken = checkToken($token);
        $admin_id = $checkToken['data']->uid;
        //获取当前管理用户扮演哪些角色
        $group_id = Db::table('think_auth_group_access')->where('uid',$admin_id)->column('group_id');
        //获取角色有哪些权限,取出权限id
        //得到一个一维数组 例如：array(0=>'1，2，3',1=>'4,5')
        $rules_arr_s = Db::table('think_auth_group')->whereIn('id',$group_id)->column('rules');
        $rules_arr = [];
        foreach ($rules_arr_s as $value) {
            //将数组循环得到的单个值用','分割成数组合并成$rules_arr，以此类推
            $rules_arr = array_merge($rules_arr, explode(',', $value));
        }
        //返回数组所有值，不保留键名,并且去除相同id
        $rules_arr = array_unique(array_values($rules_arr));
        //筛选出权限
        $think_auth_rule = Db::table('think_auth_rule')
            ->filter(function ($value){
                $value['path'] = $value['l_path'] ?? '';
                $value['name'] = $value['l_name'] ?? '';
                $value['component'] =  $value['l_component'];
                $value['meta'] = [
                    'title' => $value['title'],
                    'icon' => $value['l_icon'],
                    'roles' => ['admin'],
                ];
                return $value;
            })
            ->where('id','in',$rules_arr)
            ->select()
            ->toArray();
        $data = $this->getTree($think_auth_rule);
//        var_dump($data);
       return json(['status'=>200,'data'=>$data]);
    }
    //获取菜单【测试】
    public function getRoutesT(){
        //筛选出权限
        $think_auth_rule = Db::table('think_auth_rule')
            ->filter(function ($value){
                $value['path'] = $value['l_path'] ?? '';
                $value['name'] = $value['l_name'] ?? '';
                $value['component'] =  $value['l_component'];
                $value['meta'] = [
                    'title' => $value['title'],
                    'icon' => $value['l_icon'],
                    'roles' => ['admin'],
                ];
                return $value;
            })
            ->select()
            ->toArray();
//        var_dump($think_auth_rule);
        $data = $this->getTree($think_auth_rule);
//        var_dump($data);
        return json(['status'=>200,'data'=>$data]);
    }
    //递归
    protected function getTree($data,$f_id =0)
    {
        $list = [];
        foreach ($data as $value) {
            //第一次遍历，找到父节点为0的
            if ($value['f_id'] == $f_id) {
                //开始递归，查找父id为该节点id的节点
                if($value['level']<3) {
                    if (!empty($this->getTree($data, $value['id']))) {
                        $value['children'] = $this->getTree($data, $value['id']);
                    }
                    $list[] = $value;
                }
            }
        }
        return $list;
    }
    ////////////////////////////////////////////////
    /// 获取角色获取全部菜单
    public function getRoutes2(){
        return json(['status'=>200,'data'=>$this->getTree2(Db::table('think_auth_rule')
            ->filter(function ($value){
                $value['path'] = $value['l_path'] ?? '';
                $value['name'] = $value['l_name'] ?? '';
                $value['component'] =  $value['l_component'];
                $value['meta'] = [
                    'title' => $value['title'],
                    'icon' => $value['l_icon'],
                    'roles' => ['admin'],
                ];
                return $value;
            })->select()->toArray())]);
    }
    //递归2
    protected function getTree2($data,$f_id =0)
    {
        $list = [];
        foreach ($data as $value) {
            //第一次遍历，找到父节点为0的
            if ($value['f_id'] == $f_id) {
                //开始递归，查找父id为该节点id的节点
                if (!empty($this->getTree2($data, $value['id']))) {
                    $value['children'] = $this->getTree2($data, $value['id']);
                }
                $list[] = $value;
            }
        }
        return $list;
    }

    public function getRoutesTest(){

        $asyncRoutes = [
            [
                'path' => '/activity',
                'component' => 'Layout',
                'children' => [
                    [
                        'path' => 'activityList',
                        'name' => 'Activity',
                        'component' =>'/activity/activityList',
                        'meta' => ['title' => '活动时间', 'icon' => 'el-icon-time', 'roles' => ['editor']]
                    ]
                ]
            ],
            [
                'path' => '/tinymce',
                'component' => 'Layout',
                'children' => [
                    [
                        'path' => 'TinymceList',
                        'name' => 'Tinymce',
                        'component' =>'/tinymce/tinymceList',
                        'meta' => ['title' => 'Tinymce富文本', 'icon' => 'el-icon-document', 'roles' => ['admin']]
                    ]
                ]
            ],
            [
                'path' => '/table',
                'component' => 'Layout',
                'name' => 'Example',
                'children' => [
                    [
                        'path' => 'tableList',
                        'name' => 'Table',
                        'component' =>'/table/tableList',
                        'meta' => ['title' => '数据列表', 'icon' => 'table', 'roles' => ['admin']]
                    ]
                ]
            ],
            [
                'path' => '/seting',
                'component' => 'Layout',
                'meta' => ['title' => '管理和权限', 'icon' => 'el-icon-s-tools'],
                'children' => [
                    [
                        'path' => 'admin',
                        'name' => 'Admin',
                        'component' =>'/seting/admin/adminList',
                        'meta' => ['title' => '管理员', 'icon' => 'user', 'roles' => ['admin']]
                    ],
                    [
                        'path' => 'auth_group',
                        'name' => 'AuthGroup',
                        'component' =>'/seting/auth_group/authGroupList',
                        'meta' => ['title' => '角色设置', 'icon' => 'tree']
                    ],
                    [
                        'path' => 'auth_rule',
                        'name' => 'AuthRule',
                        'component' =>'/seting/auth_rule/authRuleList',
                        'meta' => ['title' => '权限规则', 'icon' => 'el-icon-s-flag']
                    ]
                ]
            ]
        ];
        success('获取路由',$asyncRoutes);
    }


}